#include "hole_mjs.h"
#include "mj.h"

HoleMjs::HoleMjs()
{
	
}

void HoleMjs::add_mj(Mj mj)
{
	mjs.push_back(mj);
}

int HoleMjs::peng_and_gang(Mj &mj)
{
	int num = 0;
	int i_cmp;

	for (unsigned int i=0; i<mjs.size(); i++)
	{
		i_cmp = Mj::comparemj(mjs[i], mj);
		if (i_cmp == 0)
		{
			num++;
		}
	}

	if ( num == 2 ) //两张相同
	{
		return 1;
	}
	else if ( num == 3) //三张相同
	{
		return 2;
	}
	else if ( num == 4) //四张一样的
	{
		return 3;
	}
	else
	{
		return 0;
	}
}

bool HoleMjs::check_four_same(std::vector<int> &gang_val, int dq_val)
{	
	bool flag = false;
	int select_arr[20] = {0};
	if (mjs.size() > 20)
	{
		GAME_LOG.error("check_four_same, size too big.\n");
		return false;
	}
	for (unsigned int i=0; i<mjs.size(); i++)
	{
		if (select_arr[i] == 1) 
			continue;
		
		int cnt = 0;
		for (unsigned int j=0; j<mjs.size(); j++)
		{
			if (select_arr[j] == 1 || i == j) continue;
			
			if (mjs[i].val == mjs[j].val)
			{
				select_arr[j] = 1;
				if (mjs[i].val / 10 == dq_val) //缺门不计算
				{
					cnt = 0;
				}
				else
				{
					cnt++;
				}
			}
			if (cnt == 3)
			{
				select_arr[i] = 1;
				gang_val.push_back(mjs[i].val);
				flag = true;
				break;
			}
		}
	}
	return flag;
}


bool HoleMjs::has_que_men(int type)
{
	if (type != 1 && type != 3 && type != 5)
	{
		GAME_LOG.fatal("has_que_men, type[%d], error.\n", type);
	}

	for (unsigned int i=0; i<mjs.size(); i++)
	{
		Mj mj = mjs[i];
		if (mj.val / 10 ==  type)
		{
			return true;
		}
	}
	return false;
}



